from django.http import Http404
from rest_framework import mixins, status
from rest_framework.decorators import action
from rest_framework.generics import GenericAPIView, ListAPIView, UpdateAPIView, RetrieveAPIView
from rest_framework.mixins import UpdateModelMixin, RetrieveModelMixin, ListModelMixin
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet, GenericViewSet, ViewSet, ReadOnlyModelViewSet
from rest_framework.permissions import IsAuthenticated
from apps.headline.models import Channel, Article
from apps.headline.serializer import ChannelSerializer, ArticleSerializer, ArticleDetailSerializer, \
    ArticleCreateSerializer
from apps.person.models import User

# 频道列表
from apps.qa.models import Label


class ChannelsVIew(ListAPIView):

    queryset = Channel.objects.all()
    serializer_class = ChannelSerializer
    pagination_class = PageNumberPagination


class ArticleChannelVIew(ModelViewSet):

    queryset = Article.objects.all()
    # serializer_class = ArticleSerializer
    # pagination_class = PageNumberPagination

    def get_serializer(self, *args, **kwargs):
        if self.action == 'list':
            return ArticleSerializer(*args, **kwargs)  # 文章列表
        elif self.action == 'create':
            return ArticleCreateSerializer(*args, **kwargs)  # 新增文章
        elif self.action == 'retrieve':
            return ArticleDetailSerializer(*args, **kwargs)  # 文章详情

    @action(methods=['get'],detail=False)
    def list(self,request,pk):
        if pk == '-1':
            queryset = Article.objects.all()
            serializer = self.get_serializer(queryset)

        else:
            channel = Channel.objects.get(id=pk)
            queryset = self.get_queryset().filter(channel_id=channel.id)
            serializer = self.get_serializer(instance=queryset, many=True)

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)

    @action(methods=['get'],detail=True)
    def retrieve(self, request,pk):
        aiticle = Article.objects.get(id=pk)
        serializer = self.get_serializer(aiticle)
        return Response(serializer.data)

    @action(methods=['post'],detail=False)
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)


# 收藏和取消收藏文章
class ArticleCollectView(UpdateModelMixin, GenericAPIView):
    permission_classes = [IsAuthenticated]
    queryset = User.objects.all()
    serializer_class = ArticleSerializer

    def put(self,request,pk):

        article = Article.objects.get(id=pk)
        # 当前登陆的用户
        user = request.user
        # 当前用户收藏的所有文章列表
        all_art = user.collected_articles.all()

        if article not in all_art:
            user.collected_articles.add(article)
            data = {
                'message':'收藏成功',
                'success':'true'
            }
            return Response(data)
        else:
            user.collected_articles.remove(article)
            data = {
                'message': '取消收藏成功',
                'success': 'true'
            }
            return Response(data)








